package com.example;


import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.stream.Collectors;

import static com.example.CollectorsAction.menu;
/**
 * @author Liuqz
 * @date 2018/11/18 20:41
 */
public class CollectorsAction4 {

    public static void main(String[] args) {
        testSummingDouble();
        testSummingLong();
        testSummingInt();
        testToCollection();
    }

    /**
     * result : 4200.0
     *          4200
     */
    private static void testSummingDouble() {
        System.out.println("testSummingDouble");
        Optional.ofNullable(menu.stream().collect(Collectors.summingDouble(Dish::getCalories))).ifPresent(System.out::println);

        Optional.ofNullable(menu.stream().map(Dish::getCalories).mapToInt(Integer::intValue).sum()).ifPresent(System.out::println);
    }

    /**
     * result : 4200
     */
    private static void testSummingLong() {
        System.out.println("testSummingLong");
        Optional.ofNullable(menu.stream().collect(Collectors.summingLong(Dish::getCalories))).ifPresent(System.out::println);
    }

    /**
     * result : 4200
     */
    private static void testSummingInt() {
        System.out.println("testSummingInt");
        Optional.ofNullable(menu.stream().collect(Collectors.summingInt(Dish::getCalories))).ifPresent(System.out::println);
    }

    /**
     * result : [Dish(name=pork, vegetarian=false, calories=800, type=MEAT), Dish(name=beef, vegetarian=false, calories=700, type=MEAT)]
     */
    private static void testToCollection() {
        System.out.println("testToCollection");
        Optional.ofNullable(menu.stream().filter(e -> e.getCalories() > 600).collect(Collectors.toCollection(LinkedHashSet::new))).ifPresent(System.out::println);
    }
}
